package com.avic.rong.adapter.balance;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.bean.mine.AccountBalanceBean;
import com.avic.rong.wedgit.StringUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RecvBalanceAdapter extends RecyclerView.Adapter {




    private ArrayList<AccountBalanceBean> mList;
    private Context mContext;

    public RecvBalanceAdapter(ArrayList<AccountBalanceBean> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_capital, viewGroup, false);
        return new BalanceHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        AccountBalanceBean accountBalanceBean = mList.get(i);
        BalanceHolder balanceHolder = (BalanceHolder) viewHolder;
        balanceHolder.tv_date.setText(accountBalanceBean.getChangeDesc());
        double userMoney = accountBalanceBean.getUserMoney();
        balanceHolder.tv_desc.setText(accountBalanceBean.getChangeDesc());


        String s1 = String.valueOf(userMoney);
        if (s1.startsWith("-")){
            balanceHolder.tv_price.setTextColor(mContext.getResources().getColor(R.color.black_text));
            String substring = s1.substring(1, s1.length());
            if (!substring.endsWith("0")){
                balanceHolder.tv_price.setText("-¥"+substring);
            }else {
                int integer = Double.valueOf(substring).intValue();
                balanceHolder.tv_price.setText("-¥"+integer);
            }
        }else {
            balanceHolder.tv_price.setTextColor(mContext.getResources().getColor(R.color.red_dot_color));
            if (!s1.endsWith("0")){
                balanceHolder.tv_price.setText("+¥"+s1);
            }else {
                int integer = Double.valueOf(s1).intValue();
                balanceHolder.tv_price.setText("+¥"+integer);
            }
        }

        /*int money = (int)userMoney;
        if (userMoney<0){
            Integer integer = Integer.valueOf(money);
            String s = integer.toString();
            String[] split = s.split("-");
            balanceHolder.tv_price.setText("-¥"+Integer.valueOf(split[1]));
        }else {
            balanceHolder.tv_price.setText("+¥"+money);
        }*/
        int changeTime = accountBalanceBean.getChangeTime();
        String s = String.valueOf(changeTime);
        StringBuffer stringBuffer = new StringBuffer();
        StringBuffer append = stringBuffer.append(s).append("000");
        Long aLong = Long.valueOf(append.toString());
        //long类型转日期格式
        String date = longToDate(aLong);
        balanceHolder.tv_date.setText(date);

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class BalanceHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_desc)
        TextView tv_desc;
        @BindView(R.id.tv_date)
        TextView tv_date;
        @BindView(R.id.tv_price)
        TextView tv_price;
        public BalanceHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
    public static String longToDate(long lo){
        Date date = new Date(lo);
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sd.format(date);
    }
}
